package com.iot08.admin.service;

import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.PxAdverster;
import com.iot08.common.entity.app.PxAdversterSlot;
import com.iot08.common.entity.app.PxAdvertConfiguration;
import com.iot08.common.service.base.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author cwj
 * @description
 * @date 2025/4/11 10:25
 **/
@Slf4j
@Service
public class PxAdversterSlotService extends BaseService<PxAdversterSlot> {

    @Resource
    private OperationService operationService;

    @Resource
    private PxAdversterConfigurationService pxAdversterConfigurationService;

    /**
     * @description 查询广告位
     * @author cwj
     * @param [page]
     * @return org.springframework.data.domain.Page<com.iot08.common.entity.app.PxAdversterSlot>
     * @date @time 2025/4/11 14:34
     **/
    public Page<PxAdversterSlot> list(PageVO page) {
        PageableImpl pageable = new PageableImpl();
        page.setSort(Sort.by(Sort.Order.desc("createTime")));
        pageable.setPage(page);
        Query query = new Query();
        long count = this.count(query);
        page.setTotalResult((int) count);
        List<PxAdversterSlot> list = this.find(query.with(pageable));
        Page<PxAdversterSlot> result = new PageImpl<PxAdversterSlot>(list, pageable, count);
        return result;
    }
    
    /**
     * @description 查询指定状态的广告位
     * @author cwj
     * @param [status]
     * @return java.util.List<com.iot08.common.entity.app.PxAdversterSlot>
     * @date @time 2025/4/13 16:21
     **/
    public List<PxAdversterSlot> queryAdversterSlotByStatus(Integer status) {
        Query query = new Query();
        query.addCriteria(Criteria.where("status").is(status));
        return this.find(query);
    }

    /**
     * @description 添加广告位
     * @author cwj
     * @param [pxAdversterSlot, user, ip]
     * @return void
     * @date @time 2025/4/11 15:18
     **/
    public void add(PxAdversterSlot pxAdversterSlot, SysUser user, String ip) {
        if (!ObjectUtils.isEmpty(pxAdversterSlot)  && CollectionUtils.isEmpty(getByNameOrCode(pxAdversterSlot.getName(),pxAdversterSlot.getCode()))) {
            pxAdversterSlot.setCreator(user.getAccount());
            pxAdversterSlot.setUpdator(user.getAccount());
            pxAdversterSlot.setCreateTime(new Date());
            pxAdversterSlot.setUpdateTime(new Date());
            this.save(pxAdversterSlot);
            operationService.add(user, ip, "广告位配置", "添加",
                    String.format("添加广告位配置: %s ", pxAdversterSlot.getName()));
        }
    }


    /**
     * @description 查询是否已经存在同名的广告位
     * @author cwj
     * @param [name]
     * @return com.iot08.common.entity.app.PxAdversterSlot
     * @date @time 2025/4/11 15:31
     **/
    public PxAdversterSlot getByName(String name) {
        return this.findOne(new Query(Criteria.where("name").is(name)));
    }


    /**
     * @description 查询相同编码的广告位对象
     * @author cwj
     * @param [code]
     * @return com.iot08.common.entity.app.PxAdversterSlot
     * @date @time 2025/4/14 16:28
     **/
    public  PxAdversterSlot getByCode(String code) {
        return this.findOne(new Query(Criteria.where("code").is(code)));
    }
    
    /**
     * @description 查询名称相同或编码相同的广告位
     * @author cwj
     * @param [name, code]
     * @return com.iot08.common.entity.app.PxAdversterSlot
     * @date @time 2025/4/14 16:22
     **/
    public List<PxAdversterSlot> getByNameOrCode(String name,String code) {
        Query query = new Query();
        query.addCriteria(
                new Criteria().orOperator(
                        Criteria.where("name").is(name),
                        Criteria.where("code").is(code)
                )
        );
        return this.find(query);
    }


    /**
     * @description 更新广告位信息
     * @author cwj
     * @param [pxAdversterSlot, user, ip]
     * @return void
     * @date @time 2025/4/11 17:40
     **/
    public void update(PxAdversterSlot pxAdversterSlot, SysUser user, String ip) {
        PxAdversterSlot slot = this.findById(new ObjectId(pxAdversterSlot.getId()));
        //APP标识不能重复
        if(!ObjectUtils.isEmpty(slot) && CollectionUtils.isEmpty(getByNameOrCodeAndId(pxAdversterSlot.getName(),pxAdversterSlot.getCode(),pxAdversterSlot.getId()))){
            Update update = new Update();
            update.set("name", pxAdversterSlot.getName());
            if(!StringUtils.isEmpty(pxAdversterSlot.getCode())) {
                update.set("code", pxAdversterSlot.getCode());
            }
            if(!ObjectUtils.isEmpty(pxAdversterSlot.getType())) {
                update.set("type",pxAdversterSlot.getType());
            }
            if(!ObjectUtils.isEmpty(pxAdversterSlot.getStatus())) {
                update.set("status",pxAdversterSlot.getStatus());
            }
            if(!StringUtils.isEmpty(pxAdversterSlot.getWidthHeight())) {
                update.set("widthHeight",pxAdversterSlot.getWidthHeight());
            }
            if(!ObjectUtils.isEmpty(pxAdversterSlot.getPlatform())) {
                update.set("platform",pxAdversterSlot.getPlatform());
            }
            if(!ObjectUtils.isEmpty(pxAdversterSlot.getMaxAds())) {
                update.set("maxAds",pxAdversterSlot.getMaxAds());
            }
            if(!ObjectUtils.isEmpty(pxAdversterSlot.getIsAutoRotate())) {
                update.set("isAutoRotate",pxAdversterSlot.getIsAutoRotate());
            }
            if(!ObjectUtils.isEmpty(pxAdversterSlot.getRotationInterval())) {
                update.set("rotationInterval",pxAdversterSlot.getRotationInterval());
            }
            if(!ObjectUtils.isEmpty(pxAdversterSlot.getFirstShowInterval())) {
                update.set("firstShowInterval",pxAdversterSlot.getFirstShowInterval());
            }
            if(!ObjectUtils.isEmpty(pxAdversterSlot.getShowInterval())) {
                update.set("showInterval",pxAdversterSlot.getShowInterval());
            }
            if(!ObjectUtils.isEmpty(pxAdversterSlot.getMaxFrequency())) {
                update.set("maxFrequency",pxAdversterSlot.getMaxFrequency());
            }
            if(!StringUtils.isEmpty(pxAdversterSlot.getDesc())) {
                update.set("desc", pxAdversterSlot.getDesc());
            }
            update.set("updator",user.getAccount());
            update.set("updateTime", new Date());
            this.updateById(new ObjectId(pxAdversterSlot.getId()), update);
            if((StringUtils.isEmpty(slot.getName()) && !StringUtils.isEmpty(pxAdversterSlot.getName())) ||
                    (!StringUtils.isEmpty(slot.getName()) && !slot.getName().equals(pxAdversterSlot.getName()))) {
                pxAdversterConfigurationService.updateSlotNameBySlotId(pxAdversterSlot.getId(),pxAdversterSlot.getName());
            }
            operationService.add(user, ip, "广告位配置", "编辑",
                    String.format("修改运营版广告位配置: %s ", pxAdversterSlot.getName()));
        }
    }

    /**
     * @description 查询是否存在其它同名的广告位
     * @author cwj
     * @param [name, id]
     * @return com.iot08.common.entity.app.PxAdversterSlot
     * @date @time 2025/4/11 17:38
     **/
    public PxAdversterSlot getByNameAndId(String name,String id) {
        return this.findOne(new Query(Criteria.where("_id").ne(new ObjectId(id)).and("name").is(name)));
    }

    /**
     * @description 查询广告位编码或名字相同的其它广告位
     * @author cwj
     * @param [name, code, id]
     * @return java.util.List<com.iot08.common.entity.app.PxAdversterSlot>
     * @date @time 2025/4/14 17:11
     **/
    public List<PxAdversterSlot> getByNameOrCodeAndId(String name,String code,String id){
        Query query = new Query();
        query.addCriteria(new Criteria().andOperator(Criteria.where("_id").ne(new ObjectId(id)),new Criteria().orOperator(
                Criteria.where("name").is(name),
                Criteria.where("code").is(code)
        )));
        return this.find(query);
    }
    
}
